package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.content.pm.LabeledIntent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.NewChapter;
import com.qianfeng.qlm.handforireader.util.BitmapHelp;
import com.qianfeng.qlm.handforireader.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LinXiangLin on 2015/3/25.
 */
public class NewChapterRecycleAdapter  extends RecyclerView.Adapter<NewChapterRecycleAdapter.ViewHolder> {

    private Context context;
    private List<NewChapter> data;
    private View.OnClickListener listener;

    public NewChapterRecycleAdapter(Context context, List<NewChapter> data) {
        this.context = context;
        this.data = data;

        if (context instanceof  View.OnClickListener){
            listener = (View.OnClickListener) context;
        }

        BitmapHelp.initUtils(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_new_chapter,parent,false);
        if (listener != null){
            view.setOnClickListener(listener);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (data != null){
            NewChapter newChapter = data.get(position);
            if (newChapter != null){
                String orderTitle = newChapter.getOrder_title();
                String orderName = newChapter.getOrder_name();
                String year = newChapter.getYear();
                String month = newChapter.getMonth();
                String day = newChapter.getDay();
                String picUrl = newChapter.getComic_order_pic_240_240();

                holder.txtTile.setText(orderTitle);
                holder.txtName.setText(orderName);

                if (year != null && month != null && day != null){
                    String date = year+"年"+month+"月"+day+"日";
                    holder.txtTime.setText(date);
                }

                if (picUrl != null){
                    if (!picUrl.startsWith("http://") && !picUrl.startsWith("https://")){
                        picUrl = Constant.URL_SUFFIX + picUrl;
                    }
                    BitmapHelp.getUtils().display(holder.imgIcon,picUrl,new BitmapLoadCallBack<ImageView>() {
                        @Override
                        public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                            imageView.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                            imageView.setImageDrawable(drawable);
                        }
                    });


                }
            }
        }
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if (data != null){
            size = data.size();
        }
        return size;
    }

    public void setData(List<NewChapter> data){

        if (data !=null){
            this.data.clear();
            this.data.addAll(data);
            notifyItemRangeInserted(0, data.size());
        }

    }

    public List<NewChapter> getData(){
        return data;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView imgIcon;
        private TextView txtTile;
        private TextView txtName;
        private TextView txtTime;


        public ViewHolder(View itemView) {
            super(itemView);
            imgIcon = (ImageView) itemView.findViewById(R.id.item_chapter_order_image);
            txtTile = (TextView) itemView.findViewById(R.id.item_chapter_order_title);
            txtName = (TextView) itemView.findViewById(R.id.item_chapter_order_name);
            txtTime = (TextView) itemView.findViewById(R.id.item_chapter_order_update_time);
        }
    }

}
